package java_annotation;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test {
    public static void main(String[] arge){
        DoSomething doSomething = new DoSomething();
        Method[] methods = doSomething.getClass().getDeclaredMethods(); // 反射的方式取得DoSomething类的方法

        for (Method method:methods){
            if(method.isAnnotationPresent(TestAnnotation.class)){  // 取得有注解的方法
                TestAnnotation annotation = method.getAnnotation(TestAnnotation.class); // 取得注解对象
                try {
                    method.invoke(doSomething); // 尝试执行
                    System.out.println(method.getName() + "测试正常,id="+annotation.id()+",msg="+annotation.msg());
                } catch (Exception e) {
                    System.out.println(method.getName() + "测试异常,id="+annotation.id()+",msg="+annotation.msg());
                }
            }
        }

    }



}


class DoSomething{
    @TestAnnotation(id = 1, msg = "1/0", value = {})
    public void test1 (){
        System.out.println(1/0);
    }
    @TestAnnotation(id = 2, msg = "100/1", value = {})
    public void test2 (){
        System.out.println(100/1);
    }
    public void test3 (){
        System.out.println(100/0);
    }
}